import type { PixelPoint } from "../commonTypes"
import type { Hitable } from "./hitUtil"

/**
 * 随机平滑颜色
 * @param px 
 * @param width 
 * @param height 
 * @param hitable 
 * @param color 
 */
export function smoothColorNone(px: PixelPoint, width: number, height: number,
  hitable: Hitable, color: (x: number, y: number, hitable: Hitable) => number[]) {
  return color(px.x / width, px.y / height, hitable)
}

/**
 * 随机平滑颜色
 * @param px 
 * @param width 
 * @param height 
 * @param hitable 
 * @param color 
 */
export function smoothColorRandom(px: PixelPoint, width: number, height: number,
  hitable: Hitable, color: (x: number, y: number, hitable: Hitable) => number[]) {
  const count = 50
  // 生成范围内的多个随机光线
  let resColor = [0, 0, 0]
  for (let i = 0; i < 50; i++) {
    const tmpColor = color((px.x + Math.random()) / width, (px.y + Math.random()) / height, hitable)
    resColor[0] += tmpColor[0]
    resColor[1] += tmpColor[1]
    resColor[2] += tmpColor[2]
  }
  resColor[0] /= count
  resColor[1] /= count
  resColor[2] /= count

  return resColor
}

/**
 * 原型取点平滑原色
 * @param px 
 * @param width 
 * @param height 
 * @param hitable 
 * @param color 
 * @returns 
 */
export function smoothColorRound(px: PixelPoint, width: number, height: number,
  hitable: Hitable, color: (x: number, y: number, hitable: Hitable) => number[]) {
  // 圆心取点一个
  const cColor = color(px.x / width, px.y / height, hitable)
  // 圆周半径取点8个
  // addRoundPointColor(cColor, 10, 1, px, width, height, hitable, color)
  addRoundPointColor(cColor, 10, 0.8, px, width, height, hitable, color)
  // addRoundPointColor(cColor, 10, 0.6, px, width, height, hitable, color)
  // addRoundPointColor(cColor, 10, 0.4, px, width, height, hitable, color)
  // addRoundPointColor(cColor, 10, 0.2, px, width, height, hitable, color)
  const count = 11
  cColor[0] /= count
  cColor[1] /= count
  cColor[2] /= count
  return cColor
}

function addRoundPointColor(cColor: number[], count: number, radius: number, px: PixelPoint, width: number, height: number,
  hitable: Hitable, color: (x: number, y: number, hitable: Hitable) => number[]) {
  for (let i = 0; i < Math.PI * 2; i += Math.PI * 2 / count) {
    const pColor = color(
      (px.x + Math.cos(i) * radius) / width,
      (px.y + Math.sin(i) * radius) / height,
      hitable)
    cColor[0] += pColor[0]
    cColor[1] += pColor[1]
    cColor[2] += pColor[2]
  }
  return cColor
}